package leetcode.code1604;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution2 implements IDebug {

	public List<String> alertNames(String[] keyName, String[] keyTime) {
		int len = keyName.length;
		Integer[] index = new Integer[len];
		for (int i = 0; i < index.length; i++) {
			index[i] = i;
		}
		Arrays.sort(index, (a, b) -> keyName[a].equals(keyName[b]) ? keyTime[a].compareTo(keyTime[b])
				: keyName[a].compareTo(keyName[b]));
		List<String> ans = new ArrayList<>();
		String who = "";
		boolean add = false;
		int[] times = new int[3];
		for (int i = 0, cnt = 0; i < index.length; i++) {
			String name = keyName[index[i]];
			int time = this.time(keyTime[index[i]]);
			if (name.equals(who)) {
				if (!add) {
					times[cnt % 3] = time;
					if (cnt > 1 && times[cnt % 3] - times[(cnt + 2) % 3] <= 60
							&& times[cnt % 3] - times[(cnt + 1) % 3] <= 60) {
						ans.add(name);
						add = true;
					}
					cnt++;
				}
			} else {
				add = false;
				who = name;
				cnt = 0;
				times[cnt++] = time;
			}
		}
		return ans;
	}

	private int time(String time) {
		return Integer.valueOf(time.substring(0, 2)) * 60 + Integer.valueOf(time.substring(3, 5));
	}

	@Override
	public void debug4() {
		this.alertNames(H.str2array2("[\"clare\",\"clare\",\"clare\",\"clare\"]"),
				H.str2array2("[\"18:00\",\"18:51\",\"19:30\",\"19:49\"]"));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution2 so = new Solution2();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();

	}

}
